package com.snail.jj.androiddemos.bases;

import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Window;

import com.snail.jj.androiddemos.R;
import com.snail.jj.androiddemos.tools.Configs;

import java.util.List;

/**
 * Created by snail on 2017/4/7.
 * @author snail
 */

public abstract class BaseActivity extends AppCompatActivity  {

    public String BaseTag = "BaseActivity";

    public Configs config;

    private NavigationBar navigationBar;

    public  boolean isActive = true;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);  //锁定竖屏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        config = (Configs)this.getApplication();

    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        initBaseView();
    }

    public void initBaseView() {
        navigationBar = (NavigationBar) findViewById(R.id.navigationBar);
    }

    public void setLeftDrable(Drawable drable) {
        navigationBar.setLeftImage(drable);
    }

    public void setTitleLeftText(String text) {

    }

    public void setTitleBarTitle(String title) {
        if (navigationBar != null) {
            navigationBar.setTitle(title);
        }
    }

    public void setRightViewVisiable(int visiable) {
        if (navigationBar != null) {
            navigationBar.setRightViewVisiable(visiable);
        }
    }

    public void setLeftViewVisiable(int visiable) {
        if (navigationBar != null) {
            navigationBar.setLeftViewVisiable(visiable);
        }
    }

    public void setRightText(String rTitle) {
        if (navigationBar != null) {
            navigationBar.setRightText(rTitle);
        }
    }

    public void setTitleClick(boolean canClick) {
        navigationBar.setTitleClick(canClick);
    }

    public void leftViewAction() {
        finish();
    }

    public void rightViewAction() {
        Log.i(BaseTag,"rightViewAction");
    }


    @Override
    protected void onResume() {
        super.onResume();
        if(!isActive) {
            isActive = true;
            Log.i(BaseTag,"进入前台");
        }
    }

    @Override
    protected void onStop() {
        super.onStop();

        //记录当前已经进入后台
        if (!isAppOnForeround()) {
            isActive = false;
            Log.i(BaseTag,"进入后台");
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /**
     * 程序是否在前台运行
     *
     * @author snail
     */
    public boolean isAppOnForeround() {

        ActivityManager activityManager =
                (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
        String packageName = getApplicationContext().getPackageName();

        List<ActivityManager.RunningAppProcessInfo> appProcesses =
                activityManager.getRunningAppProcesses();

        if (appProcesses == null) {
            return false;
        }

        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            if (appProcess.processName.equals(packageName)
                    && appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                return true;
            }
        }

        return false;
    }


}
